# FUnctions to create the figure that compares const vs intl 


diffmean_h2_helper <- function(data, item){

  treat_filter <- data[,"treat_const"] == 1 
  control_filter <- data[,"treat_intl"] == 1 
  treat <- subset(data, treat_filter) 
  control <- subset(data, control_filter)
  point <- mean(treat[,item], na.rm=T) - mean(control[,item], na.rm=T)
  n1 <- sum(!is.na(treat[,item]))
  n0 <- sum(!is.na(control[,item]))
  se <- sqrt(var(treat[,item], na.rm=T)/n1 + var(control[,item], na.rm=T)/n0)
  upper <- point + qnorm(0.975)*se
  lower <- point + qnorm(0.025)*se
  return(c(point, se))
}

diffmean_block <- function(data, item){
  
  data_ls <- split(data, data$block_rand)
  group_est <- as.data.frame(t(sapply(data_ls, diffmean_h2_helper,  item)))
  colnames(group_est) <- c("ate", "se")
  block_n <- as.numeric(table(data$block_rand)) # # of obs in each block
  group_est$n <- block_n
  N <- sum(group_est$n)

  # weighted sum for point estimate
  point <- sum(group_est$ate * group_est$n / N)
  # weighted sum for se
  se <- sqrt(sum(group_est$se^2 * (group_est$n/N)^2))
  
  upper <- point + qnorm(0.975)*se
  lower <- point + qnorm(0.025)*se
  p <- 2*(1-pnorm(abs(point/se)))
  return(c(lower, point, upper, p, se))
}
